<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property string $name
 * @property string $passwd
 * @property string $department
 * @property string $photo
 * @property string $phoneNum
 * @property string $descript
 *
 * The followings are the available model relations:
 * @property Logs[] $logs
 * @property Monyrec[] $monyrecs
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name', 'length', 'max'=>10),
			array('passwd', 'length', 'max'=>32),
			array('department', 'length', 'max'=>30),
			array('phoneNum', 'length', 'max'=>11),
			array('photo, descript', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name, passwd, department, photo, phoneNum, descript', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'logs' => array(self::HAS_MANY, 'Logs', 'name'),
			'monyrecs' => array(self::HAS_MANY, 'Monyrec', 'name'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'name' => '用户名',
			'passwd' => '密码',
			'department' => '部门',
			'photo' => '照片',
			'phoneNum' => '联系电话',
			'descript' => '备注',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('name',$this->name,true);
		$criteria->compare('passwd',$this->passwd,true);
		$criteria->compare('department',$this->department,true);
		$criteria->compare('photo',$this->photo,true);
		$criteria->compare('phoneNum',$this->phoneNum,true);
		$criteria->compare('descript',$this->descript,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 验证用户密码是否正确。
	 * @param string $password
	 * @return boolean
	 */
	public function validatePassword($password)
	{
		return md5($password)===$this->passwd;
	}
	
	/**
	 * 存储前将密码进行MD5加密处理
	 * @see CActiveRecord::beforeSave()
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->passwd = MD5($this->passwd );
			}
			else 
			{
				$user=Users::model()->find('LOWER(name)=?',array($this->name));
				if($user !=null)
				{
						$_tmp = MD5($this->passwd);
						if( $user->passwd != $_tmp )
						$this->passwd = $_tmp;		
				}		
			}		
			return true;
		}
		else
			return false;
	}
}